//////////////////////////////////////////////////////////////////////////////////
//                                                                              //
//  This file is part of the buola project (https://code.google.com/p/buola/).  //
//                                                                              //
//  Copyright(c) 2007-2012 Xavi Gratal                                          //
//  gratal AT gmail DOT com                                                     //
//                                                                              //
//  Buola is free software: you can redistribute it and/or modify               //
//  it under the terms of the GNU General Public License as published by        //
//  the Free Software Foundation, either version 3 of the License, or           //
//  (at your option) any later version.                                         //
//                                                                              //
//  Buola is distributed in the hope that it will be useful,                    //
//  but WITHOUT ANY WARRANTY; without even the implied warranty of              //
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               //
//  GNU General Public License for more details.                                //
//                                                                              //
//  You should have received a copy of the GNU General Public License           //
//  along with buola.  If not, see <http://www.gnu.org/licenses/>.              //
//                                                                              //
//////////////////////////////////////////////////////////////////////////////////

#include <buola/buola.h>
#include <buola/os/cenv.h>

#include <cstdlib>

#if BUOLA_PLATFORM_MAC
#include <crt_externs.h>
#else //BUOLA_PLATFORM_MAC
#include <unistd.h>
#endif //BUOLA_PLATFORM_MAC

namespace buola {

std::string CEnv::Get(const std::string &pName,const std::string &pDefault,bool pSet)
{
    char *lTmp=getenv(pName.c_str());

    if(lTmp)
        return std::string(lTmp);

    if(pSet) Set(pName,pDefault);
    return pDefault;
}

void CEnv::Set(const std::string &pName,const std::string &pValue,bool pOver)
{
    setenv(pName.c_str(),pValue.c_str(),pOver);
}

void CEnv::Unset(const std::string &pName)
{
    unsetenv(pName.c_str());
}

void CEnv::GetAll(std::map<std::string,std::string> &pMap)
{
    pMap.clear();
    
#if BUOLA_PLATFORM_MAC
    char **lEnviron=*_NSGetEnviron();
#else //BUOLA_PLATFORM_MAC
    char **lEnviron=environ;
#endif //BUOLA_PLATFORM_MAC
    
    for(char **lPtr=lEnviron;*lPtr;++lPtr)
    {
        if(!*lPtr) return;
        
        char *lVar=*lPtr;
        
        char *lEqual=strchr(lVar,'=');
        
        if(!lEqual) continue;
        
        pMap[std::string(lVar,lEqual)]=std::string(lEqual+1);
    }
}

CEnvChange::CEnvChange(const std::string &pName,const std::string &pValue)
    :   mName(pName)
{
    char *lValue=getenv(pName.c_str());
    if(lValue)
    {
        mSet=true;
        mValue=lValue;
    }
    else
    {
        mSet=false;
    }

    setenv(pName.c_str(),pValue.c_str(),1);
}

CEnvChange::~CEnvChange()
{
    if(mSet)
        setenv(mName.c_str(),mValue.c_str(),1);
    else
        unsetenv(mName.c_str());
}

/*namespace buola*/ }
